<?php
namespace NewIdiom\Controller;
use  Common\Controller\NewIdiomController;
//版本1

class IndexController extends NewIdiomController {

    public function __construct()
    {
        $sessionid = 'PHPSESSID='.$this->getSessionId();

        $getsessionid = $_SERVER['HTTP_COOKIE'];
        if ($sessionid!=$getsessionid){
            $this->returnData('','状态已过期请重新授权',400);
        }
    }


    /**
     * 获取挑战机会
     */
    public function getopportunity(){

        $member_id = I('post.member_id','','int');

        if (!$member_id){
            $this->returnData('','参数错误',401);
        }

        $date = date('Y-m-d',time());

        $where['idiom_member_id'] = $member_id;
        $where['signin_time'] = $date;

        $issign="";
        $issign = M('idiom_signin')->where($where)->getField('signin_num'); //判断今天有没有签到
        if (!$issign||$issign==""){
            $data = array(
                'idiom_member_id'=>$member_id,
                'signin_time'=>$date,
                'signin_num'=>'1'
            );
            M('idiom_signin')->data($data)->add();
            $giveopportunity = M('idiom_rule')->where(array('category'=>'idiom'))->getField('giveopportunity');
            if (empty($giveopportunity)){
                $giveopportunity = 2;
            }
            M('idiom_member')->where(array('idiom_member_id'=>$member_id))->save(array('giveopportunity'=>$giveopportunity)); //每天赠送机会
        }

        $opportunity = M('idiom_member')->field('opportunity,giveopportunity')->where(array('idiom_member_id'=>$member_id))->find();//获取挑战机会

        $chang = $opportunity['opportunity']+$opportunity['giveopportunity'];
        if ($chang>0){
            if ($opportunity['giveopportunity']>0){
                M('idiom_member')->where(array('idiom_member_id'=>$member_id))->setDec('giveopportunity'); //先减去赠送的
            }else{
                M('idiom_member')->where(array('idiom_member_id'=>$member_id))->setDec('opportunity'); //挑战机会减一
            }

            M('idiom_member')->where(array('idiom_member_id'=>$member_id))->setInc('number'); //挑战次数加一
            $this->returnData(array('opportunity'=>$chang),'成功',200);
        }

        $this->returnData(array('opportunity'=>$chang),'没有挑战机会',101);


    }

    /**
     * 更多好玩 （完成）
     */
    public function getmorepley(){

        $appid = I('post.appid','','htmlspecialchars');

        if (!$appid){
            $this->returnData('','参数错误',401);
        }

        $data = M('newidiom_moreplay')->field('appid,url')->where(array('isshow'=>'1'))->find();

        //$sharelive = M('newidiom_rule')->where(array('category'=>'idiom'))->getField('sharelive');

        $sharelive = 1; //分享复活设置

        $data['sharelive'] = $sharelive;
        $data['livecount'] = 2;

        $this->returnData($data);
    }

    /**
     * 获取用户当前机会
     */
    public function userjihui(){

        $member_id = I('post.member_id','','int');

        if (!$member_id){
            $this->returnData('','参数错误',401);
        }
        $opportunity = M('idiom_member')->field('opportunity,giveopportunity')->where(array('idiom_member_id'=>$member_id))->find();//获取挑战机会

        $chang = $opportunity['opportunity']+$opportunity['giveopportunity'];//自身加赠送的

        $this->returnData(array('opportunity'=>$chang),'成功',200);

    }

    /**
     * 获取题目进行挑战
     */

    public function challenge(){

        $member_id = I('post.member_id','','int');

        $opportunity = I('post.opportunity','','int');

        if ($member_id==""||$opportunity==""||$opportunity<1){
            $this->returnData('','没有挑战机会',101);
        }

        $nowweek = (int)date('W');
        $year = date('Y');

        $where = array(
            'year'=>$year,
            'week'=>$nowweek,
            'idiom_member_id'=>$member_id,
        );

        //判断当周是否有挑战记录
        $ischange = M('idiom_week')->field('idiom_week_id')->where($where)->find();
        if ($ischange){
            M('idiom_week')->where($where)->save(array('exittime'=>date('Y-m-d H:i:s')));
        }else{
            $where['createtime'] = date('Y-m-d H:i:s');
            $where['exittime'] = date('Y-m-d H:i:s');
            $where['getnum'] = 0;
            M('idiom_week')->data($where)->add();
        }

        //获取随机值
        $idiomquersion = $this->getidiomquersion(20);

        if ($idiomquersion){
            //msg的数字控制分享复活
            $sharelive = M('idiom_rule')->where(array('category'=>'idiom'))->getField('sharelive');
            if (empty($sharelive)&&$sharelive!='0'){
                $sharelive = 1;
            }

            $this->returnData($idiomquersion,$sharelive,200);
        }else{
            $this->returnData('','获取题目失败',100);
        }


    }

    /**
     * 答对一题
     */
    public function answerquestion(){

        $member_id = I('post.member_id','','int');
        $state = I('post.state','','int');

        if ($member_id==""||$state==""){
            $this->returnData('','参数错误',401);
        }

        $nowweek = (int)date('W');
        $year = date('Y');

        $where = array(
            'year'=>$year,
            'week'=>$nowweek,
            'idiom_member_id'=>$member_id,
        );

        //挑战成功
        if ($state=='1'){
            M('idiom_member')->where(array('idiom_member_id'=>$member_id))->setInc('score'); //分数加一
            M('idiom_week')->where($where)->setInc('getnum');//当周分数加一
            M('idiom_week')->where($where)->save(array('exittime'=>date('Y-m-d H:i:s')));
            $this->returnData('','成功',200);
        }
    }

    /**
     * 答对20题
     */
    public function pass(){
        $member_id = I('post.member_id','','int');
        $carnet = I('post.carnet','','int');
        if (!$member_id||!$carnet){
            $this->returnData('','参数错误',401);
        }
        //答对20题
        if ($carnet=='20'){
            $nowweek = (int)date('W');
            $year = date('Y');

            $where = array(
                'year'=>$year,
                'week'=>$nowweek,
                'idiom_member_id'=>$member_id,
            );
            M('idiom_week')->where($where)->setInc('getnum',6);//当周分数加6
            $songquersion['pass'] = 'successful';
            $songquersion['num'] = '25';
            M('idiom_member')->where(array('idiom_member_id'=>$member_id))->setInc('score',6); //分数加6
            $this->returnData($songquersion,'本次闯关获得25分',1);

        }
    }

    /**
     * 本周排行
     */
    public function getweek(){

        $week = $this->getcacheweek();

        $this->returnData($week);
    }

    /**
     * 上周得奖
     */
    public function lastweek(){

        //$list = $this->getlastweek();

        $list = array();

        $this->returnData($list);

    }



    /**
     * 微信群分享记录
     */
    public function sharerecord(){

        $member_id = I('post.member_id','','int');
        $shareType = I('post.shareType','2','int');
        $encryptedData = $_POST['encryptedData'];
        $iv = $_POST['iv'];


        if (!$encryptedData||!$member_id||!$iv){
            $this->returnData('','参数错误',401);
        }

        $info = M('idiom_member')->field('appid,session_key')->where(array('idiom_member_id'=>$member_id))->find();

        $appid = $info['appid'];
        $sessionkey = $info['session_key'];

        $openGId = $this->getopenGId($appid,$sessionkey,$encryptedData,$iv);

        if ($openGId=='-1'){
            $this->returnData('','分享失败',100);
        }

        $time = date('Y-m-d',time());

        $where['sharetime'] = $time;
        $where['idiom_member_id'] = $member_id;
        $where['openGId'] = $openGId;

        if ($shareType=='1'){
            $interior = $record = M('idiom_interior_group')->where($where)->getField('sharenum'); //判断是否已分享
            if ($interior){
                M('idiom_interior_group')->where($where)->setInc('sharenum');
                $this->returnData(array('repetition'=>'yes'),'今天已分享',201);
            }
            //如果没分享过则插入
            $data = array(
                'openGId'=>$openGId,
                'sharetime'=>$time,
                'sharenum'=>1,
                'idiom_member_id'=>$member_id,
            );

            M('idiom_interior_group')->data($data)->add();
            $this->returnData(array('repetition'=>'repetition'),'立即复活',200);
        }

        $where2['sharetime'] = $time;
        $where2['idiom_member_id'] = $member_id;

        $count = M('idiom_wechat_group')->where($where2)->count();

        $maxshare = M('idiom_rule')->where(array('category'=>'idiom'))->getField('maxshare');
        if (empty($maxshare)){
            $maxshare = 7;
        }

        if ($count>=$maxshare){
            $this->returnData(array('repetition'=>'out'),'一天有效分享次数为10',201);
        }


        $record = "";
        $record = M('idiom_wechat_group')->where($where)->getField('sharenum'); //判断是否已分享

        //如果已分享则自增分享次数然后返回
        if ($record){
            M('idiom_wechat_group')->where($where)->setInc('sharenum');

            $this->returnData(array('repetition'=>'yes'),'今天已分享',201);
        }


        //如果没分享过则插入，用户挑战机会加一
        $data = array(
            'openGId'=>$openGId,
            'sharetime'=>$time,
            'sharenum'=>1,
            'idiom_member_id'=>$member_id,
        );

        M('idiom_wechat_group')->data($data)->add();

        M('idiom_member')->where(array('idiom_member_id'=>$member_id))->setInc('opportunity'); //挑战机会加一

        $opportunity = M('idiom_member')->field('opportunity,giveopportunity')->where(array('idiom_member_id'=>$member_id))->find();//获取挑战机会

        $chang = $opportunity['opportunity']+$opportunity['giveopportunity'];

        $this->returnData(array('repetition'=>'no','opportunity'=>$chang),'分享成功',200);
    }

    /**
     * 获取分享内容模板 （完成）
     */
    public function getsharecontent(){
        //$sharelive = M('newidiom_rule')->where(array('category'=>'idiom'))->getField('sharelive');
        $sharelive = 0;
        $data = array(
            'title'=>'挑战你的脑力极限！！！',
            'desc'=>'挑战你的脑力极限！！！',
            'imgUrl'=>'http://oss.beikg.cn/share/share.jpg',
            'sharelive'=>$sharelive,
            'livecount'=>0,
        );

        $this->returnData($data);
    }

    /**
     * 存放模板消息 (完成)
     */
    public function send()
    {
        $appid      = I('post.appid','','htmlspecialchars');
        $touser     = I('post.touser','','htmlspecialchars');
        $page       = I('post.page','','htmlspecialchars');
        $form_id    = I('post.form_id','','htmlspecialchars');
        $form_type  = I('post.form_type','','htmlspecialchars');
        $secret     = I('post.secret','','htmlspecialchars');

        $template_id = I('post.template_id','','htmlspecialchars');

        if ($appid==""||$touser==""||$page==""||$form_id==""||$form_type==""||$secret==""||$template_id==""){
            $this->returnData('','参数错误','401');
        }

        $date = date('Y-m-d');
        $insert = array(
            'appid'=>$appid,
            'touser'=>$touser,
            'template_id'=>$template_id,
            'page'=>$page,
            'form_id'=>$form_id,
            'form_type'=>$form_type?$form_type:1,
            'status'=>'-1',
            'add_time'=>time(),
            'secret'=>$secret,
            'createtime'=>$date,
        );

        //收集多次
        M('newidiom_template_send')->data($insert)->add();
        $this->returnData('成功','200');

        $where['touser']        = $touser;
        $where['template_id']   = $template_id;
        $where['createtime']    = $date;
        $where['status']        = '-1';

        $ishave = M('newidiom_template_send')->field('wxopen_template_send_id')->where($where)->find();

        if ($ishave){
            M('newidiom_template_send')->where(array('wxopen_template_send_id'=>$ishave['wxopen_template_send_id']))->save($insert);
        }else{
            M('newidiom_template_send')->data($insert)->add();
        }

        $this->returnData('成功','200');
    }
}